//
//  CYFeedbackViewController.m
//  cyjf
//
//  Created by tauCross on 2017/8/28.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYFeedbackViewController.h"

#import <IQKeyboardManager.h>



@interface CYFeedbackViewController ()

@property(nonatomic, strong)UIView *topBackgroundView;
@property(nonatomic, strong)UITextView *textView;
@property(nonatomic, strong)UILabel *textViewPlaceholderLabel;
@property(nonatomic, strong)UILabel *lengthLabel;

@property(nonatomic, strong)UIView *bottomBackgroundView;
@property(nonatomic, strong)UITextField *contactTextField;

@property(nonatomic, strong)UIButton *submitButton;

@end


@implementation CYFeedbackViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

- (void)loadUI
{
    self.navigationItem.title = @"意见与反馈";
    
    self.tableView.hidden = YES;
    
    self.topBackgroundView = [[UIView alloc] init];
    self.topBackgroundView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.topBackgroundView];
    {
        self.textView = [[UITextView alloc] init];
        self.textView.backgroundColor = [UIColor clearColor];
        self.textView.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        self.textView.textColor = HEXCOLOR(@"444444");
        self.textView.tintColor = HEXCOLOR(@"444444");
        self.textView.textContainerInset = UIEdgeInsetsMake(15, 20, 5, 20);
        [self.topBackgroundView addSubview:self.textView];
        
        self.textViewPlaceholderLabel = [[UILabel alloc] init];
        self.textViewPlaceholderLabel.text = @"请详细描述您的问题或建议，我们将及时跟进解决。";
        self.textViewPlaceholderLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        self.textViewPlaceholderLabel.textColor = HEXCOLOR(@"999999");
        self.textViewPlaceholderLabel.numberOfLines = 0;
        [self.topBackgroundView addSubview:self.textViewPlaceholderLabel];
        
        self.lengthLabel = [[UILabel alloc] init];
        self.lengthLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        [self.topBackgroundView addSubview:self.lengthLabel];
    }
    
    self.bottomBackgroundView = [[UIView alloc] init];
    self.bottomBackgroundView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bottomBackgroundView];
    {
        self.contactTextField = [[UITextField alloc] init];
        self.contactTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号/邮箱（选填，方便我们联系你）"
                                                                                      attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14 weight:UIFontWeightRegular],
                                                                                                   NSForegroundColorAttributeName : HEXCOLOR(@"999999")}];
        self.contactTextField.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        self.contactTextField.textColor = HEXCOLOR(@"444444");
        self.contactTextField.returnKeyType = UIReturnKeyDone;
        self.contactTextField.tintColor = HEXCOLOR(@"444444");
        [self.contactTextField addTarget:self.contactTextField action:@selector(resignFirstResponder) forControlEvents:UIControlEventEditingDidEndOnExit];
        [self.bottomBackgroundView addSubview:self.contactTextField];
    }
    
    self.submitButton = [[UIButton alloc] init];
    self.submitButton.layer.masksToBounds = YES;
    self.submitButton.layer.cornerRadius = 4;
    [self.submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [self.submitButton setTitleColor:HEXCOLOR(@"FFFFFF") forState:UIControlStateNormal];
    [self.submitButton setBackgroundImage:[UIImage imageWithColor:HEXCOLOR(@"CCCCCC") size:CGSizeMake(1, 1)] forState:UIControlStateDisabled];
    [self.submitButton setBackgroundImage:[UIImage imageWithColor:APP_DEFAULT_COLOR_RED size:CGSizeMake(1, 1)] forState:UIControlStateNormal];
    [self.submitButton setBackgroundImage:[UIImage imageWithColor:HEXCOLOR(@"C63333") size:CGSizeMake(1, 1)] forState:UIControlStateHighlighted];
    self.submitButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.submitButton addTarget:self action:@selector(onSubmitButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.submitButton];
}

- (void)layoutUI
{
    self.topBackgroundView.width = self.contentView.width;
    self.topBackgroundView.height = 200;
    self.topBackgroundView.left = 0;
    self.topBackgroundView.top = 10;
    {
        self.textView.width = self.topBackgroundView.width;
        self.textView.height = self.topBackgroundView.height - 40;
        self.textView.left = 0;
        self.textView.top = 0;
        
        self.textViewPlaceholderLabel.size = [self.textViewPlaceholderLabel sizeThatFits:CGSizeMake(self.textView.width - 50, MAXFLOAT)];
        self.textViewPlaceholderLabel.left = self.textView.left + 25;
        self.textViewPlaceholderLabel.top = self.textView.top + 15;
        
        [self.lengthLabel sizeToFit];
        self.lengthLabel.right = self.topBackgroundView.width - 15;
        self.lengthLabel.centerY = self.topBackgroundView.height - 20;
    }
    
    self.bottomBackgroundView.width = self.contentView.width;
    self.bottomBackgroundView.height = 50;
    self.bottomBackgroundView.left = 0;
    self.bottomBackgroundView.top = self.topBackgroundView.bottom + 10;
    {
        self.contactTextField.width = self.bottomBackgroundView.width - 40;
        self.contactTextField.height = self.bottomBackgroundView.height;
        self.contactTextField.centerX = self.bottomBackgroundView.width / 2;
        self.contactTextField.top = 0;
    }
    
    self.submitButton.width = self.contentView.width - 40;
    self.submitButton.height = 44;
    self.submitButton.centerX = self.contentView.width / 2;
    self.submitButton.top = self.bottomBackgroundView.bottom + 40;
}

- (void)loadData
{
    @weakify(self);
    [self.textView.rac_textSignal subscribeNext:^(id x) {
        @strongify(self);
        self.textViewPlaceholderLabel.hidden = self.textView.text.length;
        
        NSInteger length = self.textView.text.length;
        self.lengthLabel.text = [NSString stringWithFormat:@"%li/250", (long)length];
        self.lengthLabel.textColor = length > 250 ? APP_DEFAULT_COLOR_RED : HEXCOLOR(@"999999");
        [self.lengthLabel sizeToFit];
        self.lengthLabel.right = self.topBackgroundView.width - 15;
        self.lengthLabel.centerY = self.topBackgroundView.height - 20;
        
        self.submitButton.enabled = (self.textView.text.length > 0 && self.textView.text.length <= 250);
    }];
}

#pragma mark - Actions
- (void)submitAction
{
    [self.view endEditing:YES];
    [SVProgressHUD show];
    @weakify(self);
    [CYStandardDataAPI user_reportSuggestWithContent:self.textView.text contactWay:self.contactTextField.text complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            [SVProgressHUD showSuccessWithStatus:@"信息提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:response.message];
        }
    }];
}

#pragma mark - Control Events
- (void)onSubmitButtonTouchUpInside
{
    [self submitAction];
}

@end
